from flask import render_template,request
from . import bcjl
from common.base import get_mysql_data as get_data
# from common.base import print_file,doc_file

import os,time

@bcjl.route('/')
def bcjl_index():
    return render_template('bcjl_index.html')

# @bcjl.route('/mzcf',methods=['GET','POST'])
# def mzcf():
    file_base_path = os.path.join(os.path.dirname(__file__),'datas')
    filename = os.path.join(file_base_path,'清风宝处方笺.docx')        
    if request.method == 'POST' and len(request.form)>0:
        zyhs = request.form.get('sql')
        sql = f'''
            select 
            a.c_code '就诊号',
            a.c_name '姓名',
            c.性别 '性',
            a.c_age_desc '年龄',
            ifnull(e.c_addr_d,e.c_address) '地址',
            c.门诊诊断 '诊断',
            concat('20',mid(a.c_code,1,2),'-',mid(a.c_code,3,2),'-',mid(a.c_code,5,2)) '日期',
            d.d_name '医师'
            from mz_p_code a
            left join mz_p_cost2 b on a.c_code=b.c_code 
            left join mz_p_jzxx c on a.c_code=c.就诊号
            left join doctor_id d on a.c_doctor_id=d.doctor_id
            left join mz_p_jbxx e on a.c_code=e.c_code
            where a.c_code in ({zyhs})
        '''
        list = get_data(sql)
        if list:
            for a in list:
                for b in a:
                    if not a[b]:
                        a[b]='' 
            for item in list:            
                filename2 = os.path.join(file_base_path,'mzcf',time.strftime(f'%Y%m%d-{item.get('姓名')}.docx'))
                doc_file(filename2,filename,item)
                print_file(filename2)
                time.sleep(1)
 


 